package huadi.sso.client.listener;

import huadi.sso.client.filter.AuthenticationFilter;

import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * SSO client session listener. Be in charge of deleting the session from the
 * filter's map.
 * 
 * @author Huadi
 * 
 */
public class SsoClientSessionListener implements HttpSessionListener {

	private Map<String, HttpSession> idToSessionMapping;

	public void sessionCreated(HttpSessionEvent se) {
	}

	public void sessionDestroyed(HttpSessionEvent se) {
		if (idToSessionMapping == null) {
			synchronized (SsoClientSessionListener.class) {
				if (idToSessionMapping == null) {
					idToSessionMapping = AuthenticationFilter.getSessionMap();
				}
			}
		}
		idToSessionMapping.remove(se.getSession().getId());
	}

}
